#include "bsp_adc.h"
#include "systick.h"
#include "bsp_usart.h"
#include "main.h"
static uint16_t adc_value[ADC_DMA_NUM_MAX];
 
const tsADCChannel ADCChannelTable[ADC_NUM_MAX]={	
	
	ADC_CHANNEL_OVER_WATER,/*水箱溢水位*/				
	ADC_CHANNEL_PURE_TDS,	/*纯水TDS*/			
	ADC_CHANNEL_SOURCE_TDS,/*源水*/  
	ADC_CHANNEL_LEAK,			/*漏水*/				
	ADC_CHANNEL_12,/*热罐低水位*/		
	ADC_CHANNEL_13,/*热罐中水位*/	
	ADC_CHANNEL_0,/*热罐高水位*/	 			
};	

const tsADCChannel ADCDMAChannelTable[ADC_DMA_NUM_MAX]={	
	ADC_CHANNEL_14,	//热罐NTC			
	ADC_CHANNEL_7,	//纯水NTC		
	ADC_CHANNEL_15, //出水NTC
	ADC_CHANNEL_10,	//增压泵电流ADC	  
	ADC_CHANNEL_8,	//热水泵电流		
	ADC_CHANNEL_9,  //常温水泵电流
  ADC_CHANNEL_1,  //UV电流采样
};
static void delayMM(uint16_t nus)
{
	for(uint16_t i = 0; i < nus; i++)
	{
		for(uint16_t j = 0; j < 5000; j++)
		{
			j++;j--;j++;j--;
		}
	}
}
uint16_t OnReadADCDMAvalue(uint32_t channle)
{
		/******************DMA**********************/		
//		adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);
//	  while(!adc_flag_get(ADC0, ADC_FLAG_EOC));
//    /* clear the end of conversion flag */
//    adc_flag_clear(ADC0, ADC_FLAG_EOC);
//		print_debug("shuchu :%d\r\n",adc_value[0]);
	  return adc_value[channle];
}

uint16_t OnReadADCvalue(uint32_t channle)
{	
/*****************非DMA**************TDS检测使用**********/	 	
	uint64_t tempTick = HAL_GetTickMs();

	adc_regular_channel_config(ADC1 , 0U, ADCChannelTable[channle].channel, ADC_SAMPLETIME_239POINT5);
  adc_software_trigger_enable(ADC1, ADC_REGULAR_CHANNEL);
	   /* wait the end of conversion flag */
  while(!adc_flag_get(ADC1, ADC_FLAG_EOC)&& ((HAL_GetTickMs() - tempTick) < 10 ));//最长时间10ms超时 && ((HAL_GetTickMs() - tempTick) < 10)
    /* clear the end of conversion flag */
  adc_flag_clear(ADC1, ADC_FLAG_EOC);
	return adc_regular_data_read(ADC1);

}

void MX_ADC_Init(void)
{
	 /* enable GPIOC clock */
    rcu_periph_clock_enable(RCU_GPIOC);
	 /* enable ADC0 clock */
    rcu_periph_clock_enable(RCU_ADC0);  //用于非TDS类  采样DMA模式
		rcu_periph_clock_enable(RCU_ADC1);  //用于TDS类
	/* config ADC clock */
    rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV6);
	 /* enable DMA clock */
    rcu_periph_clock_enable(RCU_DMA0);
	
	 /* config the GPIO as analog mode */  
	  gpio_init(ADC_NTC_HEAT_GPIO_Port, 			GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_NTC_HEAT_Pin);           //热水温度NTC
	  gpio_init(ADC_NTC_PURE_GPIO_Port, 			GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_NTC_PURE_Pin);					 //纯水温度NTC
		gpio_init(ADC_NTC_WATER_OUT_GPIO_Port, 	GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_NTC_WATER_OUT_Pin);			 //出水温度NTC
		gpio_init(ADC_OVER_TDS_GPIO_Port,       GPIO_MODE_AIN, 		GPIO_OSPEED_MAX,    ADC_OVER_TDS_Pin);					 //纯水箱溢水tds_ADC
		gpio_init(ADC_PURE_TDS_GPIO_Port,       GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_PURE_TDS_Pin);				   //纯水tds_ADC
		gpio_init(ADC_SOURCRE_TDS_GPIO_Port, 		GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_SOURCRE_TDS_Pin);				 //源水tds_ADC
		gpio_init(ADC_LEAK_TDS_GPIO_Port, 			GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_LEAK_TDS_Pin);					 //漏水tds_ADC
	
		gpio_init(ADC_HEAT_L_TDS_GPIO_Port, 		GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_HEAT_L_TDS_Pin);					   //热罐低  临时
		gpio_init(ADC_HEAT_M_TDS_GPIO_Port, 		GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_HEAT_M_TDS_Pin);					   //热罐高  临时
		gpio_init(ADC_HEAT_H_TDS_GPIO_Port, 		GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_HEAT_H_TDS_Pin);					   //热罐中  临时


		gpio_init(ADC_BOOST_PUMP_GPIO_Port, 		GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_BOOST_PUMP_Pin);				 //增压泵ADC
		gpio_init(ADC_NORMAL_PUMP_GPIO_Port, 		GPIO_MODE_AIN, 		GPIO_OSPEED_MAX,	  ADC_NORMAL_PUMP_Pin);        //常温水泵ADC
		gpio_init(ADC_HAET_PUMP_GPIO_Port, 			GPIO_MODE_AIN, 		GPIO_OSPEED_MAX,	  ADC_HAET_PUMP_Pin);        	 //热水泵ADC
		//UV灯ADC

		/* reset ADC */
    adc_deinit(ADC0); 
		adc_deinit(ADC1);
	/* configure the ADC sync mode */
    adc_mode_config(ADC_MODE_FREE);  
		/* ADC contineous function enable */
    adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, ENABLE);   //DMA的模式需要连续转换
    /* ADC scan mode function enable */
    adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);
		adc_special_function_config(ADC1, ADC_SCAN_MODE, ENABLE);//用于TDS类
    /* ADC data alignment config */
    adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
		adc_data_alignment_config(ADC1, ADC_DATAALIGN_RIGHT);//用于TDS类
    /* ADC channel length config */
    adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL,ADC_DMA_NUM_MAX); //
		adc_channel_length_config(ADC1, ADC_REGULAR_CHANNEL,1);//用于TDS类
//    /* ADC regular channel config */
    for(uint8_t i=0;i<ADC_DMA_NUM_MAX; i++ )
		adc_regular_channel_config(ADC0 , i , ADCDMAChannelTable[i].channel , ADC_SAMPLETIME_239POINT5);  //用于DMA
		
    /* ADC external trigger enable */
    adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);
		adc_external_trigger_config(ADC1, ADC_REGULAR_CHANNEL, ENABLE);
    /* ADC trigger config */
    adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_2_EXTTRIG_REGULAR_NONE);
    adc_external_trigger_source_config(ADC1, ADC_REGULAR_CHANNEL, ADC0_1_2_EXTTRIG_REGULAR_NONE);
    /* enable ADC interface */
    adc_enable(ADC0);
		adc_enable(ADC1);
    delayMM(1);    
    /* ADC calibration and reset calibration */
    adc_calibration_enable(ADC0);
		adc_calibration_enable(ADC1);
		
		
/***************************************************************************/		
    /* ADC DMA function enable */
    adc_dma_mode_enable(ADC0);
		
		/* ADC_DMA_channel configuration */
    dma_parameter_struct dma_data_parameter;
    
    /* ADC_DMA_channel deinit */
    dma_deinit(DMA0, DMA_CH0);
    
    /* initialize DMA single data mode */
    dma_data_parameter.periph_addr	 =	(uint32_t)(&ADC_RDATA(ADC0));
    dma_data_parameter.periph_inc 	 =	DMA_PERIPH_INCREASE_DISABLE;
    dma_data_parameter.memory_addr 	 =	(uint32_t)(&adc_value);
    dma_data_parameter.memory_inc 	 =	DMA_MEMORY_INCREASE_ENABLE;
    dma_data_parameter.periph_width  =	DMA_PERIPHERAL_WIDTH_16BIT;
    dma_data_parameter.memory_width  =	DMA_MEMORY_WIDTH_16BIT;
    dma_data_parameter.direction 		 =	DMA_PERIPHERAL_TO_MEMORY;
    dma_data_parameter.number 			 =	ADC_DMA_NUM_MAX;                       //转换数量
    dma_data_parameter.priority 		 =	DMA_PRIORITY_HIGH;  
    dma_init(DMA0, DMA_CH0, &dma_data_parameter);
  
    dma_circulation_enable(DMA0, DMA_CH0);
		
//		//中断管理器开启通道4中断
//	nvic_irq_enable(DMA0_Channel0_IRQn, 0, 1);	
//	/* dam 转换结束产生中断*/
//	dma_interrupt_enable(DMA0, DMA_CH0, DMA_INT_FTF);
	
    /* enable DMA channel */
    dma_channel_enable(DMA0, DMA_CH0);  
		
		adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);
}



void test_adc(void)
{
	UV_LIGHT_ON();
	print_debug("ADC_I_UV_LIGHT: %d \r\n",OnReadADCDMAvalue(ADC_I_UV_LIGHT));  // 0.32v  401  OFF 0v  0
//	UV_LIGHT_OFF();
//	print_debug("ADC_I_UV_LIGHT: %d \r\n",OnReadADCDMAvalue(ADC_I_UV_LIGHT));
	
//	  NORMAL_WATER_BUMP_PWM_PLUSE(100);
//	print_debug("ADC_I_NORMAL_PUMP: %d \r\n",OnReadADCDMAvalue(ADC_I_NORMAL_PUMP));
//	print_debug("ADC_I_HEAT_PUMP: %d \r\n",OnReadADCDMAvalue(ADC_I_HEAT_PUMP));

//		HAL_GPIO_WritePin(HEAT_L_TDS_FWD_GPIO_Port,		  HEAT_L_TDS_FWD_Pin,	  GPIO_PIN_SET); 
//		HAL_GPIO_WritePin(HEAT_M_TDS_FWD_GPIO_Port,		  HEAT_M_TDS_FWD_Pin,	  GPIO_PIN_SET);
//		HAL_GPIO_WritePin(HEAT_H_TDS_FWD_GPIO_Port ,		HEAT_H_TDS_FWD_Pin,	  GPIO_PIN_SET);
//		HAL_GPIO_WritePin(HEAT_COM_TDS_FWD_GPIO_Port,		HEAT_COM_TDS_FWD_Pin,	GPIO_PIN_RESET);    //高中低共用一个探针
//	print_debug("ADC_TDS_HEAT_L: %d \r\n",OnReadADCvalue(ADC_TDS_HEAT_L));
//	print_debug("ADC_TDS_HEAT_M: %d \r\n",OnReadADCvalue(ADC_TDS_HEAT_M));
//	print_debug("ADC_TDS_HEAT_H: %d \r\n",OnReadADCvalue(ADC_TDS_HEAT_H));
	
//	uint16_t buf[ADC_NUM_MAX];
////	print_debug("I_HEAT_PUMP: %d \r\n",OnReadADCvalue(ADC_I_HEAT_PUMP));//热水泵
////	print_debug("I_NORMAL_PUMP: %d \r\n",OnReadADCvalue(ADC_I_NORMAL_PUMP));//常温水泵
//	
//  for(uint8_t i=0;i<ADC_NUM_MAX; i++)
//	{
//		 buf[i]=OnReadADCvalue(i);
//		print_debug("buf[%d]: %d \r\n",i,buf[i]);
//		float vref_value = buf[i] * 3.3 / 4096;
//    
//		switch(i)
//		{
//			case ADC_NTC_HEAT:    print_debug("NTC_HEAT: %f \r\n",vref_value);    
//			break;
//			case ADC_NTC_PURE:		print_debug("NTC_PURE: %f \r\n",vref_value); 
//			break;
//			case ADC_NTC_WATER_OUT:print_debug("NTC_WATER_OUT: %f \r\n",vref_value); 
//			break;
//			case ADC_TDS_OVER_WATER:print_debug("TDS_OVER_WATER: %f \r\n",vref_value); 
//			break;
//			case ADC_TDS_PURE:print_debug("TDS_PURE: %f \r\n",vref_value); 
//			break;
//			case ADC_TDS_SOURCE:print_debug("TDS_SOURCE: %f \r\n",vref_value); 
//			break;
//			case ADC_TDS_LEAK:print_debug("TDS_LEAK: %f \r\n",vref_value); 
//			break;
//					
//			case ADC_I_BOOST_PUMP:print_debug("I_BOOST_PUMP: %f \r\n",vref_value); 
//			break;
//			case ADC_I_HEAT_PUMP:print_debug("I_HEAT_PUMP: %f \r\n",vref_value); 
//			break;
//			case ADC_I_NORMAL_PUMP:print_debug("I_NORMAL_PUMP: %f \r\n",vref_value); 
//			break;
//			default:
//				break;
//		}
//	}
	
}
